package Hash;

import java.security.PublicKey;
import java.util.HashMap;

public class HashBuck2 <K,V>{

    static class Node<K,V>{
        public K key;
        public V values;
        public Node<K,V> next;

        public Node(K key, V values) {
            this.key = key;
            this.values = values;
        }
    }

public Node<K,V>[]array=new Node[10];
    public int useSize=0;
    public static final double DEFAULT_LOAD_FACTOR=0.75;


public void put(K key,V values){
    int k=key.hashCode()% array.length;
    Node cur=array[k];
    while(cur!=null){
        if(cur.key==key){
            cur.values=values;
            return;
        }
        cur=cur.next;
    }
    Node node=new Node(key,values);
    node.next=array[k];
    array[k]=node;
    useSize++;

    if (doLodeFactor()>DEFAULT_LOAD_FACTOR){
resize();
    }
}

private double doLodeFactor(){
    return useSize*1.0/ array.length;
}

private void resize(){
    Node<K,V>[]newArray=new Node[array.length*2];
    for (int i = 0; i < array.length ; i++) {
        Node<K,V>cur=array[i];
        while(cur!=null){
            int k=cur.key.hashCode()% newArray.length;
            Node<K,V>curN=cur.next;
            cur.next=newArray[k];
newArray[k]=cur;
cur=curN;
        }
    }
    array=newArray;
}


public V getValues(K key){
    int k=key.hashCode()% array.length;
Node<K,V>cur=array[k];
while(cur!=null){
    if(cur.key==key){
        return cur.values;
    }
    cur=cur.next;
}
return null;
}
}
